<!DOCTYPE html>
<html lang="en">

	<head>
		<meta charset="UTF-8">
		<meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no">
		<meta name="apple-mobile-web-app-capable" content="yes">
		<meta name="apple-mobile-web-app-status-bar-style" content="black">
		<meta name="format-detection" content="telephone=no">
		<title>无忧微贷</title>
		<link rel="stylesheet" type="text/css" href="../css/reset.css">
		<link rel="stylesheet" type="text/css" href="../css/common.css">
		<link rel="stylesheet" type="text/css" href="../css/icomoon/style.css">
		<script type="text/javascript" src="../js/rem.js"></script>
	</head>

	<body>
		<div class="m-common m-securitypwd m-recharge m-withdrawal">
			<h3 class="header f-pr">
				<span>提现</span>
				<i class="icon-angle-left f-pa"></i>
				<small class="f-pa instruction">提现说明</small>
			</h3>
			<div class="bankinfo f-pr clearfix">
				<div id="bank">
					<div class="banklogo f-fl"><img id="banklogo" src="../images/recharge-icon-1.png"></div>
					<div class="info">
						<p id="bankname">未知</p>
						<p id="bank_no">暂无</p>
					</div>
				</div>
				<i class="icon-angle-right f-pa"></i>
			</div>
			<p class="tips">
				<i class="warning"></i>
				<span>可提现余额<span id="balance">0.00</span>元</span>
				<button class="all f-fr" id="all">全部提现</button>
			</p>
			<ul>
				<li>
					<label>提现金额</label>
					<input type="text" class="amount" placeholder="建议输入100的整数倍金额" id="amount">
					<span class="f-pa">元</span>
					<i class="close-icon f-pa"></i>
				</li>
				<!--<li>
					<label>手续费</label>
					<input type="text" class="fee" placeholder="提现手续费" id="fee" value="2" readonly="true">
					<span class="f-pa">元</span>
				</li>-->
				<li>
					<label>到账金额</label>
					<input type="text" class="realamount" placeholder="提现到账金额" id="realamount" value="" readonly="readonly">
					<span class="f-pa">元</span>
				</li>
				<li>
					<label>交易密码</label>
					<input type="password" class="amount" placeholder="输入交易密码" id="password">
					<i class="close-icon f-pa"></i>
				</li>
			</ul>
			<!--<p class="fee f-fr">手续费：<span>2</span>元</p>-->
			<div class="btn-box">
				<a class="btn" id="cash">提现</a>
			</div>
		</div>
		<input type="hidden" id="balance_decimal" />
		<script type="text/javascript" src="../js/md5.min.js"></script>
		<script type="text/javascript" src="../js/zepto.min.js"></script>
		<script type="text/javascript" src="../js/mui.min.js"></script>
		<script type="text/javascript" src="../js/common.js"></script>
		<script type="text/javascript">
			var page = {
				init: function() {
					mui.ajax(base + 'account/availablebalance', {
						type: 'get',
						data: {
							'id': parseInt(plus.storage.getItem('user_id'))
						},
						dataType: 'json',
						headers: sign(md5),
						success: function(data) {
							console.log('余额:' + JSON.stringify(data));
							$('#balance').html(data.balance);
							$('#balance_decimal').val(data.balance_decimal);
						},
						error: function(XMLHttpRequest, textStatus, error) {
							console.error('可提现余额错误信息：' + XMLHttpRequest.status);
						}
					});
				},
				bankcard: function() {
					mui.ajax(base + 'account/bankcard', {
						type: 'get',
						data: {
							'user': parseInt(plus.storage.getItem('user_id'))
						},
						dataType: 'json',
						headers: sign(md5),
						success: function(data) {
							console.log('银行卡:' + JSON.stringify(data));
							$('#bankname').html(data.bank_name);
							$('#bank_no').html(data.bank_no);
							$('#banklogo').attr('src', banklogo(data.bank_code));
						},
						error: function(XMLHttpRequest, textStatus, error) {
							console.error('银行卡错误信息：' + XMLHttpRequest.status);
						}
					});
				},
				all: function() {
					$('#all').on('tap', function() {
						var balance = $('#balance_decimal').val();
						var cashamount = (parseFloat(balance) - 0).toFixed(2);
						$('#amount').val(cashamount);
						$('#realamount').val(cashamount);
					});
				},
				back: function() {
					$('.icon-angle-left').on('tap', function() {
						mui.back();
					});
				},
				
				instruction: function(){
					$('.instruction').on('tap', function(){
						mui.openWindow({
							url: 'withdrawalinstruction.html',
							show: {
								aniShow: 'pop-in', 
							    event: "loaded"
							}
						});
					});
				},

				fee: function() {
					$('#amount').keyup(function(e) {
						//大于0且最多两位小数的正数
						var regex = /^([1-9][0-9]*(\.[0-9]{1,2})?|0\.(?!0+$)[0-9]{1,2})$/;
						if(!regex.test($(this).val())) {
							mui.toast('充值金额格式不正确');
							return false;
						} else {
							var amount = parseFloat($(this).val());
							if(amount + 0 > parseFloat($('#balance_decimal').val())) {
								var cashamount = (parseFloat($('#balance_decimal').val()) - 2).toFixed(2);
								$('#amount').val(cashamount);
								$('#realamount').val(cashamount);
							} else {
								$('#realamount').val(amount);
							}
						}
					});
				},

				cash: function() {
					$('#cash').bind('tap', function() {
						var amount = $('#amount').val();
						//大于0且最多两位小数的正数
						var regex = /^([1-9][0-9]*(\.[0-9]{1,2})?|0\.(?!0+$)[0-9]{1,2})$/;
						if(!regex.test(amount)) {
							mui.toast("提现金额格式不正确");
							return false;
						}
						// 对比提现金额与可提现余额
//						if(amount + 2 > parseFloat($('#balance_decimal').val())) {
//							mui.toast('可提现余额小于提现余额加手续费');
//							return false;
//						}
						//验证支付密码
						var pwd = $('#password').val();
						pwd = pwd.replace(/\s/g, '');
						if(pwd === '') {
							mui.toast("支付密码不能为空");
							return false;
						}
						mui.ajax(base + 'account/cash', {
							data: {
								'user': parseInt(plus.storage.getItem('user_id')),
								'amount': amount,
								'borrow_id': '',
								'password': pwd
							},
							dataType: 'json',
							type: 'post',
							success: function(data) {
								console.log('提现结果：' + JSON.stringify(data));
								if(data.result) {
									mui.openWindow({
										url: 'withdrawalsuccess.html',
										extras: {
											amount: amount
										}
									});
									//重新加载个人中心数据
									plus.webview.getWebviewById('html/account.html').evalJS('init();');
								} else {
									mui.openWindow({
										url: 'withdrawalerror.html',
										extras: {
											message: data.message
										}
									});
								}
							},
							error: function(data) {
								console.log(JSON.stringify(data))
							}
						})
					});
				},
				
				choosebank: function(){
					$('#bank').on('tap', function(){
						mui.openWindow({
							url: 'withdrawalbank.html',
							show: {
								aniShow: 'pop-in', 
							    event: "loaded"
							}
						});
					});
				}
			};

			document.addEventListener("plusready", function() {
				page.back();
				page.init();
				page.bankcard();
				page.cash();
				page.all();
				page.fee();
				page.instruction();
				page.choosebank();
			}, false);
		</script>
	</body>

</html>